<template>
  <el-card>
    <el-form label-width="120px" :model="formData" :rules="rules" ref="addForm">
      <el-form-item label="属性名" prop="specsname">
        <el-input placeholder="请输入属性名" v-model="formData.specsname"></el-input>
      </el-form-item>
      <el-form-item label="属性值" prop="attrs">
        <div class="attr-item" v-for="(item,index) in attrList" :key="index">
          <el-input placeholder="请输入属性值" v-model="item.value"></el-input>
          <el-button type="success" v-if="index==0" @click="addAttr">添加</el-button>
          <el-button type="danger" v-else @click="delAttr(index)">移除</el-button>
        </div>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="formData.status">
          <el-radio :label="1">正常</el-radio>
          <el-radio :label="2">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button @click="submitForm" type="primary">立即添加</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
export default {
  data() {
    //   自定义表单校验规则
    var validateAttr= (rule, value, callback) => {
        // console.log(this.attrList);
        this.attrList.forEach(item=>{
            if(item.value==''){
                callback(new Error('请输入属性值!'));
            }
        });
        // 无论成功与否, 都应该调用callback
         callback();
      };  
    return {
      // 定义数据模型
      formData: {
        specsname: "", //属性名称
        attrs: "", // 属性值
        status: 1 // 状态
      },
      attrList: [{ value: "" }],
      // 表单校验规则
      rules: {
        specsname: { required: true, message: "请输入属性名", trigger: "blur" },
        //自定义的表单校验规则
        attrs: { validator: validateAttr, trigger: "blur" },
        status: { required: true, message: "请选择状态", trigger: "blur" }
      }
    };
  },
  methods: {
      addAttr(){
          if(this.attrList.length>=5){
              return false;
          }
          this.attrList.push({value:''});
      },
      delAttr(index){
          this.attrList.splice(index,1);
      },
    //   提交表单
      submitForm(){
          this.$refs.addForm.validate(async valid=>{
              if(valid){
                this.formData.attrs=this.attrList.map(item=>item.value).join(',');
                const res=await this.$http.post('/api/specsadd',this.formData);
                if(res.code==200){
                  this.$router.push('/specs');
                  return  this.$success('添加成功');
                }
                this.$error('添加失败');
              }
          });
      },
      resetForm(){
          this.$refs.addForm.resetFields();
          this.attrList=[{ value: "" }];
      }
  }
};
</script>

<style scoped>
/* 
样式添加作用域的原理:
给style标签添加scoped属性之后, 当前组件中样式选择器会被改写,改写成一个交集选择器, 类似这样.box => .box[data-v-7059db36]
 */
.el-form {
  width: 600px;
  margin: auto;
}
.attr-item {
  display: flex;
  width: 400px;
  margin:5px 0;
  justify-content: space-between;
}
.attr-item .el-input{
    width:320px;
}
</style>